<template>
	<view class="comment">
		<view class="comment-header">
			<view class="comment-header-img">
				<image :src="item.author.avator" mode="aspectFill"></image>
			</view>
			<view class="comment-header-info">
				<view v-if="!item.is_reply" class="comment-header-info-title">
					{{item.author.author_name}}
				</view>
				<view v-else class="comment-header-info-title">
					{{item.author.author_name}} <text class="reply-text">回复</text> {{item.to}}
				</view>
				<view>
					{{item.create_time | formatTime}}
				</view>
			</view>
		</view>
		<view class="comment-content">
			<view>{{item.comment_content}}</view>
			<view class="comment-button-warp">
				<view class="comment-button" @click="commentReply({comments:item,is_reply:reply})">回复</view>
			</view>
			<view class="comment-reply" v-for="(i,index) in item.replys" :key='index'>
				<comment-box :reply='true' :item='i' @reply='commentReply'></comment-box>
			</view>
		</view>
	</view>
</template>

<script>
	import commentBox from '@/components/comment-box/comment-box.vue'
	import {parseTime} from '@/utils/index.js'
	export default {
		name:"comment-box",
		components:{
			commentBox
		},
		props:{
			item:{
				type:Object,
				default(){
					return {}
				}
			},
			reply:{
				type:Boolean,
				default:false
			}
		},
		// 计算属性
		filters:{
			formatTime(time){
				return parseTime(time)
			}
		},
		data() {
			return {
				
			};
		},
		methods:{
			commentReply(comment){
				// 为了区分主回复,还是子回复
				if(comment.is_reply){
					comment.comments.reply_id=comment.comments.comment_id
					comment.comments.comment_id=this.item.comment_id
				}
				this.$emit('reply',comment)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.comment{
		margin: 15px 0;
		.comment-header{
			display: flex;
			.comment-header-img{
				flex-shrink: 0;
				width: 30px;
				height: 30px;
				border-radius: 5px;
				overflow: hidden;
				image{
					width: 100%;
					height: 100%;
				}
			}
			.comment-header-info{
				display: flex;
				flex-direction: column;
				padding-left: 15px;
				font-size: 12px;
				color: #999;
				line-height: 1;
				.comment-header-info-title{
					margin-bottom: 10px;
					font-size: 14px;
					color: #333;
					.reply-text{
						margin: 0 10px;
						font-weight: bold;
						color: #000;
					}
				}
			}
		}
		.comment-content{
			margin-top: 10px;
			font-size: 14px;
			color: #000;
			.comment-button-warp{
				margin-top: 15px;
				display: flex;
				.comment-button{
					padding: 2px 10px;
					font-size: 12px;
					color: #999;
					border-radius: 20px;
					border: 1px solid #ccc;
				}
			}
			.comment-reply{
				display: flex;
				margin: 15px 0;
				padding: 0 10px;
				border: 1px solid #eee;
			}
		}
	}
</style>
